<template>
  <div class="page">
    <div class="dis_container">
      <el-main>
        <div class="login-area login-half left password-login-wrapper">
          <div class="maintext">用户注册</div>
          <hr class="line" />
          <el-form :model="registerInfo" :rules="rules" ref="form" class="login-form top-label-form" accept-charset="UTF-8">
            <el-form-item prop="userName">
              <el-input
                class="username"
                type="text"
                v-model="registerInfo.userName"
                autocomplete="off"
                placeholder="账号"
                :maxlength="20"
                :clearable="true"
              />
            </el-form-item>
            <el-form-item prop="password">
              <div id="login-password">
                <el-input
                  class="password"
                  v-model="registerInfo.password"
                  autocomplete="off"
                  placeholder="密码"
                  show-password
                  :maxlength="20"
                  :clearable="true"
                />
              </div>
            </el-form-item>
            <el-form-item prop="passwordConfirm">
              <div id="login-password">
                <el-input
                  class="password"
                  v-model="registerInfo.passwordConfirm"
                  autocomplete="off"
                  placeholder="确认密码"
                  show-password
                  :maxlength="20"
                  :clearable="true"
                />
              </div>
            </el-form-item>
            <div>
              <button @click="onRegister" id="log-in-button">
                <span class="label">注册</span>
              </button>
              <button @click="onCancel" id="log-in-button">
                <span class="label">取消</span>
              </button>
            </div>
          </el-form>
        </div>
      </el-main>
    </div>
  </div>
</template>
<script>
import { register } from '@/api/user.js'
export default {
  name: "Login",
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (value.length < 8) {
          callback(new Error('密码长度需不小于8位'));
        } 
        if (this.registerInfo.passwordConfirm !== '') {
          this.$refs.form.validateField('passwordConfirm');
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerInfo.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      //任务表单
      registerInfo: {
        userName: "",
        password: "",
        passwordConfirm: ""
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ validator: validatePass, trigger: "blur" },
        ],
        passwordConfirm: [{ validator: validatePass2, trigger: "blur"},
        ],

      },
      //参数传递
      loginstatus: "",
      msg: "",
    };
  },
  created() {},
  methods: {
    onCancel() {
      this.$router.push("/login");
    },
    onRegister() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let formData = new FormData();
          formData.append('userName', this.registerInfo.userName);
          formData.append('password', this.registerInfo.password);
          register(formData).then((res) => {
            if (res.data.code === 20000) {
              this.$message.success('注册成功！');
              this.$router.push("/login");
            } else {
              this.$message.error('用户名已存在！');
            }
          }).catch((error) => {
            console.log(error);
          })
        } else {
          this.$message({
            message: '提交失败，请仔细检查内容！',
            type: 'warning',
          })
          return false;
        }
      })
    }
  },
};
</script>
<style scoped>
/deep/ .el-input__inner {
  height: 52px !important;
}
/deep/ .el-form-item__error {
  margin-left: 50px;
}
.zctext {
  margin-bottom: 25px;
  height: 45px;
  width: 80%;
  margin: 0 10%;
}
.label_left {
  float: left;
  color: #696969;
}
.label {
  font-size: 18px;
}
.label_right {
  float: right;
}
.username {
  /*no-repeat:图片不重复；10px：控制图片左边距 center：图片上下居中*/
  background: url(../../assets/username.png) no-repeat 10px center !important;
  padding: 0px 50px; /*  输入内容左右边距控制 */
  width: 80%;
  height: 52px;
  /* line-height: 5%; */
  font-size: 16px;
  color: #696969;
  border-color: #333b4f;
  margin-bottom: 10px;
  margin-top: 30px;
}
.password {
  background: url(../../assets/password.png) no-repeat 10px center;
  padding: 0px 50px;
  width: 80%;
  height: 52px;
  font-size: 16px;
  color: #696969;
  border-color: #333b4f;
  margin-bottom: 10px;
}
.maintext {
  font-family: "Microsoft YaHei";
  font-size: 22px;
  font-weight: bold;
  color: #333b4f;
}
.line {
  color: #333b4f;
  border: none;
  border-top: 1px solid;
}
.dis_container {
  display: flex;
  flex-direction: row;
  -webkit-box-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  align-items: center;
  max-width: 45%;
  box-sizing: border-box;
  padding-top: 4%;
  padding-bottom: 6%;
  margin: 0px auto;
  clear: both;
}
.db {
  height: 100%;
  width: 100%;
  font-size: 12px;
  color: #ffffff;
  padding-bottom: 10%;
  padding-top: 1%;
}

.el-main {
  background-color: #ffffff;
  color: #333;
  text-align: center;
  max-width: 500px;
  height: 550px;
  border-radius: 8px;
  border-style: solid;
  border-width: 5px;
  border-color: rgba(255, 255, 255, 0.3);
  background-clip: padding-box;
  overflow: auto; 
}

.password-login-wrapper {
  text-align: center;
}
.login-half left {
  padding: 1% 0;
  width: 100% !important;
  display: block;
}
.login-area,
.login-form top-label-form {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  display: block;
}
.top-label-form label {
  font-weight: 700;
  display: block;
  margin: 0 0 3px;
  letter-spacing: 0.5px;
}
label[for],
input[type="checkbox"],
input[type="radio"] {
  cursor: pointer;
}
#log-in-button {
  width: 80%;
  font-size: 18px;
  font-weight: bold;
  font-family: "Microsoft YaHei";
  line-height: 1.2;
  text-overflow: ellipsis;
  background: #4388f5;
  color: #fff;
  padding: 10px;
  margin-top: 10px;
  border-radius: 10px;
  border-width: 1px;
  border-color: aliceblue;
  cursor: pointer;
}
input {
  width: 50%;
  border-radius: 5px;
}
.page {
  background: url(../../assets/login-left.png) no-repeat 0% 100%,
    url(../../assets/login-right.png) no-repeat 100% 0%,
    linear-gradient(to bottom, #486cc0, #203187);
  height: 100vh;
}
</style>